﻿using System;
using System.Collections;
using System.Collections.Generic;

namespace AntBot
{
	public class SortedLocList : ICollection
	{
		private readonly List<Location> mInnerList;
		private readonly IComparer<Location> mComparer;

		public SortedLocList()
			:this(10)
		{}

		public SortedLocList(int capacity)
		{
			mInnerList = new List<Location>(capacity);
			mComparer = new LocationComparer();
		}

		public void Add(Location item)
		{
			int index = mInnerList.BinarySearch(item, mComparer);
			if (index < 0)
				mInnerList.Insert(~index, item);
		}

		public void RemoveAt(int index)
		{
			mInnerList.RemoveAt(index);
		}

		public int IndexOf(Location item)
		{
			int index = mInnerList.BinarySearch(item, mComparer);
			return index >= 0 ? index : -1;
		}

		public Location Get(int index)
		{
			return mInnerList[index];
		}

		public void Remove(Location location)
		{
			int index = IndexOf(location);
			if (index != -1)
				mInnerList.RemoveAt(index);
		}

		public void Clear()
		{
			mInnerList.Clear();
		}

		#region IEnumerable members
		public IEnumerator GetEnumerator()
		{
			return mInnerList.GetEnumerator();
		}
		#endregion

		#region ICollection members
		public void CopyTo(Array array, int index)
		{
			throw new NotImplementedException();
		}

		public int Count
		{
			get { return mInnerList.Count; }
		}

		public object SyncRoot
		{
			get { throw new NotImplementedException(); }
		}

		public bool IsSynchronized
		{
			get { throw new NotImplementedException(); }
		}
		#endregion

		#region Inner Classes
		internal class LocationComparer : IComparer<Location>
		{
			#region IComparer Members
			public int Compare(Location a, Location b)
			{
				if (a.GetHashCode() > b.GetHashCode()) return 1;
				if (a.GetHashCode() < b.GetHashCode()) return -1;
				return 0;
			}
			#endregion
		}
		#endregion
	}
}
